RAIS  3.2
C:/Projekte/RAIS/common/UserData.cs
Go to the documentation of this file.
00001 
00028 using System;
00029 using System.Collections.Generic;
00030 using System.Data;
00031 using System.Text;
00032 using System.Security.Cryptography;
00033 
00034 using RAIS.Common.MessageManagement;
00035 
00036 namespace RAIS.Common.UserManagement
00037 {
00038     #region UserData class
00039 
00042     public class UserData : DBObject
00043     {
00044         //*********************************************************************
00045         #region Enumerators
00046         public enum DataRoleIndication
00047         {
00048             DataRole,
00049             Facility
00050         }
00051         public enum UserType
00052         {
00053             Administrator,
00054             Regulator,
00055             RestrictedAccessRegulator,
00056             Licensee,
00057             Guest,
00058             Other
00059         }
00060         public enum UserStatus
00061         {
00062             Active,
00063             NotActive,
00064             LoggedOn,
00065             Locked,
00066             LoggedOff
00067         }
00068         #endregion
00069         //*********************************************************************
00070         #region Fields
00071         static Dictionary<string, UserTypeData> userTypesDictionary; 
00072         static Dictionary<UserStatus, UserStatusData> userStatusesDictionary; 
00073 
00074         string login = string.Empty;                              
00075         string password = null;                         
00076         //UserType userType = UserType.Guest;               ///<User type
00077         UserType userType = UserType.Other;                 
00078         UserStatus userStatus = UserStatus.NotActive;   
00079         string email = string.Empty;                                        
00080         string completeName = string.Empty;                             
00081         string addressLine1;                                                                    
00082         string addressLine2;                                                                    
00083         string addressLine3;                                                                    
00084         string city;                                                                                    
00085         string postCode;                                                                                
00086         DataRoleIndication dataRoleType = DataRoleIndication.DataRole;  
00087         FunctionRole functionalRole;                                    
00088         DataRole dataRole;                                                              
00089         Facility facility;                                                              
00090         string selectedLanguage = Common.Constants.DEFAULT_LANGUAGE; 
00091         string accountRequestComment;                                    
00092         Message accountRequest;                                                 
00093         #endregion
00094         //*********************************************************************
00095         #region Properties
00096 
00101         public static Dictionary<string, UserTypeData> UserTypes
00102         {
00103             get { return userTypesDictionary; }
00104             set { userTypesDictionary = value; }
00105         }
00111         public static Dictionary<UserStatus, UserStatusData> UserStatuses
00112         {
00113             get { return userStatusesDictionary; }
00114             set { userStatusesDictionary = value; }
00115         }
00116 
00117         new public string Name
00118         {
00119             get { return this.name; }
00120             set
00121             {
00122                 this.name = value;
00123                 this.completeName = value;
00124             }
00125         }
00129         public string Login
00130         {
00131             get { return this.login; }
00132             set { this.login = value; }
00133         }
00137         public string Password
00138         {
00139             get { return this.password; }
00140             set { this.password = HashPassword(value); }
00141         }
00145         public UserTypeData Type
00146         {
00147             get { return userTypesDictionary[this.TypeText]; }
00148         }
00152         public string TypeText
00153         {
00154             get
00155             {
00156                 string result = string.Empty;
00157                 foreach (KeyValuePair<string, UserTypeData> userTypeItem in userTypesDictionary)
00158                     if (userTypeItem.Value.Type == this.userType)
00159                     {
00160                         result = userTypeItem.Value.VisibleName;
00161                         break;
00162                     }
00163                 return result;
00164             }
00165             set { this.userType = userTypesDictionary[value].Type; }
00166         }
00170         public UserStatus Status
00171         {
00172             get { return this.userStatus; }
00173             set { this.userStatus = value; }
00174         }
00178         public UserStatusData StatusData
00179         {
00180             get { return userStatusesDictionary[this.userStatus]; }
00181         }
00185         public bool Locked
00186         {
00187             get { return (this.Status == UserStatus.Locked); }
00188         }
00192         public string Email
00193         {
00194             get { return this.email; }
00195             set { this.email = value; }
00196         }
00200         public string CompleteName
00201         {
00202             get { return this.completeName; }
00203             set
00204             {
00205                 this.name = value;
00206                 this.completeName = value;
00207             }
00208         }
00212         public string AddressLine1
00213         {
00214             get { return addressLine1; }
00215             set { addressLine1 = value; }
00216         }
00220         public string AddressLine2
00221         {
00222             get { return addressLine2; }
00223             set { addressLine2 = value; }
00224         }
00228         public string AddressLine3
00229         {
00230             get { return addressLine3; }
00231             set { addressLine3 = value; }
00232         }
00236         public string City
00237         {
00238             get { return city; }
00239             set { city = value; }
00240         }
00244         public string PostCode
00245         {
00246             get { return postCode; }
00247             set { postCode = value; }
00248         }
00252         public bool FacilitySpecified
00253         {
00254             get { return this.dataRoleType == DataRoleIndication.Facility; }
00255         }
00259         public bool DataRoleSpecified
00260         {
00261             get { return this.dataRoleType == DataRoleIndication.DataRole; }
00262         }
00266         public int DataRoleType
00267         {
00268             get { return (int)this.dataRoleType; }
00269             set { this.dataRoleType = (DataRoleIndication)value; }
00270         }
00274         public DataRole DataRole
00275         {
00276             get { return this.dataRole; }
00277             set { this.dataRole = value; }
00278         }
00282         public Facility Facility
00283         {
00284             get { return this.facility; }
00285             set { this.facility = value; }
00286         }
00290         public FunctionRole FunctionalRole
00291         {
00292             get { return this.functionalRole; }
00293             set { this.functionalRole = value; }
00294         }
00298         public string Language
00299         {
00300             get { return this.selectedLanguage; }
00301             set { this.selectedLanguage = value; }
00302         }
00308         public bool Validator
00309         {
00310             get { return (this.functionalRole.Validation == FunctionRole.ValidationType.Validates); }
00311         }
00315         public string AccountRequestComment
00316         {
00317             get { return accountRequestComment; }
00318             set { accountRequestComment = value; }
00319         }
00323         public Message AccountRequest
00324         {
00325             get { return accountRequest; }
00326             set { accountRequest = value; }
00327         }
00331         public bool IsActive
00332         {
00333             get;
00334             set;
00335         }
00336         #endregion
00337         //*********************************************************************
00338         #region Constructors
00339         public UserData()
00340         { }
00341         public UserData(IDataRecord record)
00342         {
00343             this.ID = (int)record["PK User ID"];
00344             this.Name = record["Complete Name"].ToString();
00345             this.login = record["User Name"].ToString();
00346             this.password = record["Password"].ToString();
00347             this.completeName = record["Complete Name"].ToString();
00348             this.email = record["Email"].ToString();
00349             this.addressLine1 = record["Address Line 1"].ToString();
00350             this.addressLine2 = record["Address Line 2"].ToString();
00351             this.addressLine3 = record["Address Line 3"].ToString();
00352             this.city = record["City"].ToString();
00353             this.postCode = record["Post Code"].ToString();
00354             this.TypeText = record["User Type Visible Name"].ToString();
00355             this.userStatus = (UserStatus)Enum.Parse(typeof(UserStatus), record["User Status Name"].ToString());
00356             if (record["PK Functional Role ID"].ToString() != "")
00357                 this.functionalRole = new FunctionRole(record);
00358             if (record["PK Data Role ID"].ToString() != "")
00359                 this.dataRole = new DataRole(record);
00360             if (record["PK Facility ID"].ToString() != "")
00361                 this.facility = new Facility(record);
00362             this.dataRoleType = (this.dataRole == null) ? DataRoleIndication.Facility : DataRoleIndication.DataRole;
00363         }
00364         #endregion
00365         //*********************************************************************
00366         #region Public methods
00367         public bool HasAccessToCategory(string categoryName)
00368         {
00369             return this.FunctionalRole.ContainsCategoryRestriction(categoryName) ||
00370                     this.FunctionalRole.ContainsCategoryRestriction(RAIS.Common.Constants.ALL_CATEGORIES);
00371         }
00372         public static string HashPassword(string pass)
00373         {
00374             byte[] passArray = Encoding.UTF8.GetBytes(pass);
00375             byte[] hashArray;
00376 
00377             using (HashAlgorithm alg = new SHA256Managed())
00378                 hashArray = alg.ComputeHash(passArray, 0, passArray.Length);
00379 
00380             return Convert.ToBase64String(hashArray);
00381         }
00382 
00383         public override string ToString()
00384         {
00385             return this.login;
00386         }
00387         #endregion
00388     }
00389     #endregion
00390     //*********************************************************************
00391     #region Nested classes
00392     public class UserTypeData
00393     {
00394         //*********************************************************************
00395         #region Fields
00396         int id;
00397         string name;
00398         string visibleName;
00399         #endregion
00400         //*********************************************************************
00401         #region Properties
00402         public int ID
00403         {
00404             get { return this.id; }
00405         }
00406         public UserData.UserType Type
00407         {
00408             get { return (UserData.UserType)Enum.Parse(typeof(UserData.UserType), this.name); }
00409         }
00410         public string VisibleName
00411         {
00412             get { return this.visibleName; }
00413         }
00414         #endregion
00415         //*********************************************************************
00416         #region Constructors
00417         public UserTypeData(IDataRecord record)
00418         {
00419             this.id = (int)record["PK User Type ID"];
00420             this.name = record["User Type Name"].ToString();
00421             this.visibleName = record["User Type Visible Name"].ToString();
00422         }
00423         #endregion
00424         public string GetDefaultDataRole()
00425         {
00426             string ret = null;
00427             switch (Type)
00428             {
00429                 case UserData.UserType.Administrator:
00430                     ret = "All data";
00431                     break;
00432                 case UserData.UserType.Regulator:
00433                     ret = "All data";
00434                     break;
00435                 case UserData.UserType.RestrictedAccessRegulator:
00436                     ret = "All data";
00437                     break;
00438                 case UserData.UserType.Licensee:
00439                     ret = null;
00440                     break;
00441                 case UserData.UserType.Guest:
00442                     ret = "Guest access";
00443                     break;
00444                 case UserData.UserType.Other:
00445                     ret = null;
00446                     break;
00447             }
00448             return ret;
00449         }
00450         public string GetDefaultFunctionalRole()
00451         {
00452             string ret = null;
00453             switch (Type)
00454             {
00455                 case UserData.UserType.Administrator:
00456                     ret = "Administrator";
00457                     break;
00458                 case UserData.UserType.Regulator:
00459                     ret = "Regulator";
00460                     break;
00461                 case UserData.UserType.RestrictedAccessRegulator:
00462                     ret = "Restricted Access Regulator";
00463                     break;
00464                 case UserData.UserType.Licensee:
00465                     ret = "Licensee";
00466                     break;
00467                 case UserData.UserType.Guest:
00468                     ret = "Guest";
00469                     break;
00470                 case UserData.UserType.Other:
00471                     ret = null;
00472                     break;
00473             }
00474             return ret;
00475         }
00476         public bool IsFacility()
00477         {
00478             bool ret = false;
00479             switch (Type)
00480             {
00481                 case UserData.UserType.Licensee:
00482                     ret = true;
00483                     break;
00484                 case UserData.UserType.Other:
00485                     ret = true;
00486                     break;
00487             }
00488             return ret;
00489         }
00490         public bool IsDataRole()
00491         {
00492             bool ret = false;
00493             switch (Type)
00494             {
00495                 case UserData.UserType.Other:
00496                     ret = true;
00497                     break;
00498             }
00499             return ret;
00500         }
00501     }
00502     public class UserStatusData : DBTypeObject
00503     {
00504         //*********************************************************************
00505         #region Properties
00506         public UserData.UserStatus Status
00507         {
00508             get { return (UserData.UserStatus)Enum.Parse(typeof(UserData.UserStatus), this.name); }
00509         }
00510         #endregion
00511         //*********************************************************************
00512         #region Constructors
00513         public UserStatusData()
00514         { }
00515         public UserStatusData(IDataRecord record)
00516         {
00517             this.id = (int)record["PK User Status ID"];
00518             this.name = record["User Status Name"].ToString();
00519             this.visibleName = record["User Status Visible Name"].ToString();
00520         }
00521         #endregion
00522     }
00523     #endregion
00524 }